package tp.badugi.client.guiViews;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

import tp.badugi.client.GuiClient;

public class CardChangeView extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7754566762396572664L;

	private GuiClient client;
	
	private final Dimension cardExchangeViewSize = new Dimension (300,200);
	
	private int cardButtonHeight;
	
	private int cardButtonWidth;
	
	private final Dimension gameViewSize = new Dimension(800, 600);
	
	public final Color cardDefaultButtonColor;
	
	private JPanel cardPanel = new JPanel();
	private JPanel cardButtonPanel = new JPanel();
	
	public JButton cardButton[];
	public JButton cardExchangeButton = new JButton("Exchange");
	public JButton cardCancelButton = new JButton("Cancel");
	
	public JLabel cardTimerLabel = new JLabel();
	
	public Timer cardTimer;
	
	public final int cardTimeout;
	
	public int cardTimeoutCount;
	
	public CardChangeView(GuiClient client,String[] cards, int timeout) {
		
		this.client = client;
		
		this.cardTimeout = timeout/1000;

		this.cardDefaultButtonColor = this.cardExchangeButton.getBackground();
		
		this.makeView();
		this.refreshView(cards);
	}
	
	private void makeView(){
		int numberOfCards = 4;
		
		this.cardTimer = new Timer(1000, this.client);
		
		this.setBounds(
				(int)(this.client.windowBoundPoint.getX() + (this.gameViewSize.getWidth()-this.cardExchangeViewSize.getWidth())/2),
				(int)(this.client.windowBoundPoint.getY() + (this.gameViewSize.getHeight()-this.cardExchangeViewSize.getHeight())/2),
				(int)this.cardExchangeViewSize.getWidth(), (int)this.cardExchangeViewSize.getHeight());
		
		this.cardPanel.setBackground(Color.BLUE);
		
		this.cardPanel.setLayout(new BorderLayout());
		this.cardPanel.setBorder(new EmptyBorder(10, 10, 10, 10) );
		this.add(this.cardPanel);
		
		
		this.cardButtonPanel.setLayout(new GridLayout(1,numberOfCards));
		this.cardButton = new JButton[numberOfCards];
		
		for (int i = 0; i < numberOfCards; i++){
			this.cardButton[i] = new JButton();
			this.cardButton[i].addActionListener(this.client);
			this.cardButtonPanel.add(this.cardButton[i]);
		}
		
		this.add(this.cardButtonPanel, BorderLayout.CENTER);
		this.add(this.cardExchangeButton, BorderLayout.SOUTH);
		this.add(this.cardCancelButton, BorderLayout.NORTH);
		this.add(this.cardTimerLabel, BorderLayout.EAST);
		
		this.cardExchangeButton.addActionListener(this.client);
		this.cardCancelButton.addActionListener(this.client);		

		this.setUndecorated(true);
		this.setVisible(true);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		this.cardButtonHeight = this.cardButtonPanel.getHeight();
		this.cardButtonWidth = this.cardButtonPanel.getWidth()/4;		
	}
	
	public void refreshView(String[] cards){
		
		Image image;
		Image scaledImage;
		
		this.cardTimeoutCount = this.cardTimeout;
		
		for (int i = 0; i < cards.length; i++){
			image = this.client.imageProvider.getCardImage(cards[i]);
			scaledImage = image.getScaledInstance(this.cardButtonWidth,this.cardButtonHeight, Image.SCALE_SMOOTH);
			this.cardButton[i].setIcon(new ImageIcon(scaledImage));
			this.cardButton[i].setBackground(this.cardDefaultButtonColor);
			this.cardButton[i].setBorder(new EmptyBorder(0,0,0,0));
		}
		
		this.cardTimerLabel.setText(String.valueOf(this.cardTimeout));
		
		this.cardTimer.start();
	}
}
